home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / ppm / spctoppm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  4.5 KB  |  212 lines

  1. /* spctoppm.c - read a compressed Spectrum file and produce a portable pixmap
  2. **
  3. ** Copyright (C) 1991 by Steve Belczyk and Jef Poskanzer
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14.  
  15. #define ROWS 200
  16. #define COLS 320
  17. #define MAXVAL 7
  18.  
  19. static void DoBitmap ARGS(( FILE* ifp ));
  20. static void DoChar ARGS(( int n, char c ));
  21. static void DoColormap ARGS(( FILE* ifp ));
  22.  
  23. static char screen[ROWS*COLS/2];
  24. static short sscreen[ROWS*COLS/4];
  25. static pixel pal[ROWS][48];
  26. static long colormap_length, bitmap_length;
  27.  
  28. void
  29. main( argc, argv )
  30.     int argc;
  31.     char* argv[];
  32.     {
  33.     FILE* ifp;
  34.     char c1, c2;
  35.     pixel* pixelrow;
  36.     register pixel* pP;
  37.     int row, col;
  38.  
  39.     ppm_init( &argc, argv );
  40.  
  41.     /* Check args. */
  42.     if ( argc > 2 )
  43.     pm_usage( "[spcfile]" );
  44.  
  45.     if ( argc == 2 )
  46.     ifp = pm_openr( argv[1] );
  47.     else
  48.     ifp = stdin;
  49.  
  50.     /* Check SPC file header. */
  51.     c1 = getc( ifp );
  52.     c2 = getc( ifp );
  53.  
  54.     if ( ( c1 != 'S' ) || ( c2 != 'P' ) )
  55.     pm_error( "not a Spectrum picture" );
  56.  
  57.     /* Skip reserved bytes. */
  58.     getc( ifp );
  59.     getc( ifp );
  60.  
  61.     /* Get length of bitmap data. */
  62.     (void) pm_readbiglong( ifp, &bitmap_length );
  63.  
  64.     /* and colormap */
  65.     (void) pm_readbiglong( ifp, &colormap_length );
  66.  
  67.     /* Process bitmap. */
  68.     DoBitmap( ifp );
  69.  
  70.     /* Process colormap. */
  71.     DoColormap( ifp );
  72.  
  73.     pm_close( ifp );
  74.  
  75.     /* Write the PPM file. */
  76.     ppm_writeppminit( stdout, COLS, ROWS, (pixval) MAXVAL, 0 );
  77.     pixelrow = ppm_allocrow( COLS );
  78.  
  79.     for ( row = 0; row < ROWS; ++row )
  80.     {
  81.     for ( col = 0, pP = pixelrow; col < COLS; ++col, ++pP )
  82.         {
  83.         int c, ind, b, plane, x1;
  84.  
  85.         /* Compute pixel value. */
  86.         ind = ( 80 * row ) + ( ( col >> 4 ) << 2 );
  87.         b = 0x8000 >> (col & 0xf);
  88.         c = 0;
  89.         for ( plane = 0; plane < 4; ++plane )
  90.         if ( b & sscreen[ind+plane] )
  91.             c |= (1 << plane);
  92.  
  93.         /* Compute palette index. */
  94.         x1 = 10 * c;
  95.         if ( c & 1 )
  96.         x1 -= 5;
  97.         else
  98.         ++x1;
  99.         if ( ( col >= x1 ) && ( col < ( x1 + 160 ) ) )
  100.         c += 16;
  101.         if ( col >= ( x1 + 160 ) )
  102.         c += 32;
  103.  
  104.         /* Store the proper color. */
  105.         *pP = pal[row][c];
  106.         }
  107.     ppm_writeppmrow( stdout, pixelrow, COLS, (pixval) MAXVAL, 0 );
  108.     }
  109.  
  110.     pm_close( stdout );
  111.  
  112.     exit( 0 );
  113.     }
  114.  
  115. static void
  116. DoBitmap( ifp )
  117.     FILE* ifp;
  118.     {
  119.     int i;
  120.     long count, data;
  121.     char h, c;
  122.  
  123.     /* Zero out first scan line. */
  124.     for ( i = 0; i < 160; ++i )
  125.     screen[i] = 0;
  126.  
  127.     /* 'count' counts number of input bytes. */
  128.     count = 0;
  129.  
  130.     /* 'data' counts just data bytes. */
  131.     data = 0;
  132.  
  133.     while ( count < bitmap_length )
  134.     {
  135.     /* Get next record header. */
  136.     h = getc( ifp );
  137.     ++count;
  138.  
  139.     if ( ( h >= 0 ) && ( count < bitmap_length ) )
  140.         {
  141.         for ( i = 0; i <= h; ++i )
  142.         {
  143.         c = getc( ifp );
  144.         ++count;
  145.         DoChar( data, c );
  146.         ++data;
  147.         }
  148.         }
  149.     else if ( ( h < 0 ) && ( count < bitmap_length ) )
  150.         {
  151.         c = getc( ifp );
  152.         ++count;
  153.  
  154.         for ( i = 0; i < ( 2 - h ); ++i )
  155.         {
  156.         DoChar( data, c );
  157.         ++data;
  158.         }
  159.         }
  160.     }
  161.  
  162.     /* Convert the char version of the screen to short. */
  163.     for ( i = 0; i < ROWS*COLS/4; ++i )
  164.     sscreen[i] = ( screen[i<<1] << 8 ) + ( 0xff & screen[(i<<1)+1] );
  165.     }
  166.  
  167. #if __STDC__
  168. static void
  169. DoChar( int n, char c )
  170. #else /*__STDC__*/
  171. static void
  172. DoChar( n, c )
  173.     int n;
  174.     char c;
  175. #endif /*__STDC__*/
  176.     {
  177.     int i;
  178.  
  179.     /* Compute screen index. */
  180.     i = 160 + 2 * ( n / 7960 ) + 8 * ( ( n % 7960 ) / 2 ) + ( n & 1 );
  181.     screen[i] = c;
  182.     }
  183.  
  184. static void
  185. DoColormap( ifp )
  186.     FILE* ifp;
  187.     {
  188.     int i, j, b;
  189.     short mask;
  190.  
  191.     /* Clear first three palettes. */
  192.     for ( j = 0; j < 48; ++j )
  193.     PPM_ASSIGN( pal[0][j], 0, 0, 0 );
  194.  
  195.     /* Read the palettes. */
  196.     for ( i = 1; i < ROWS; ++i )
  197.     for ( j = 0; j < 3; ++j )
  198.         {
  199.         (void) pm_readbigshort( ifp, &mask );
  200.         for ( b = 0; b < 15; ++b )
  201.         if ( mask & ( 1 << b ) )
  202.             {
  203.             short k;
  204.             (void) pm_readbigshort( ifp, &k );
  205.             PPM_ASSIGN( pal[i][(j*16)+b],
  206.             ( k & 0x700 ) >> 8,
  207.             ( k & 0x070 ) >> 4,
  208.             ( k & 0x007 ) );
  209.             }
  210.         }
  211.     }
  212.